/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor.controller;

import java.awt.event.ActionEvent;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.veditor.VirtualEditor;
import offset.nodes.client.editor.controller.EditorActions;
import offset.nodes.client.editor.controller.TableActions;
import offset.nodes.client.editor.model.TableModel;
import offset.nodes.client.veditor.model.DataModel;

/**
 *
 * @author
 * Walter
 * Lütgenau
 */
public class VirtualEditorActions extends EditorActions {

    public static class VirtualInsertBreakAction extends InsertBreakAction {

        public VirtualInsertBreakAction(Editor editor) {
            super(editor);
        }

        public void action(ActionEvent e) throws Exception {
            DataModel dataModel = new DataModel(getDocumentContext(e), getEditor().getProperties());

            int pos = getEditorPane(e).getCaretPosition();

            if (dataModel.isInTemplate(pos)) {
                new DataActions.ShowViewAction((VirtualEditor) getEditor()).action(e);
                return;
            }

            super.action(e);
        }
    }

    public static class VirtualTabAction extends TabAction {

        public VirtualTabAction(Editor editor) {
            super(editor);
        }

        public void action(ActionEvent e) throws Exception {
            DataModel dataModel = new DataModel(getDocumentContext(e), getEditor().getProperties());

            int pos = getEditorPane(e).getCaretPosition();

            if (dataModel.isInTemplate(pos)) {
                new DataActions.TabAction((VirtualEditor) getEditor()).action(e);
                return;
            }

            super.action(e);
        }
    }

    public static class VirtualBackTabAction extends BackTabAction {

        public VirtualBackTabAction(Editor editor) {
            super(editor);
        }

        public void action(ActionEvent e) throws Exception {
            DataModel dataModel = new DataModel(getDocumentContext(e), getEditor().getProperties());

            int pos = getEditorPane(e).getCaretPosition();

            if (dataModel.isInTemplate(pos)) {
                new DataActions.BackTabAction((VirtualEditor) getEditor()).action(e);
                return;
            }

            super.action(e);
        }
    }
    
    public static class VirtualCaretLeftAction extends CaretLeftAction {
        static DataActions.CommonCaretAction dataCaretAction = null;        

        public VirtualCaretLeftAction(Editor editor) {
            super(editor);
            dataCaretAction = new DataActions.CommonCaretAction((VirtualEditor) editor, "data-caret-left");            
        }

        public void action(ActionEvent e) throws Exception {
            DataModel dataModel = new DataModel(getDocumentContext(e), getEditor().getProperties());

            int pos = getEditorPane(e).getCaretPosition();

            getDefaultCaretLeftAction().actionPerformed(e);

            if (getEditor().getMode() == VirtualEditor.MODE_VIRTUAL_PAGE_DATA_EDITOR && dataModel.isInTemplate(pos)) {
                dataCaretAction.adjustDataFocus();
                return;
            }
            
            getEditor().fireUserCaretEvent();
        }
    }

    public static class VirtualCaretRightAction extends CaretRightAction {

        static DataActions.CommonCaretAction dataCaretAction = null;

        public VirtualCaretRightAction(Editor editor) {
            super(editor);
            dataCaretAction = new DataActions.CommonCaretAction((VirtualEditor) editor, "data-caret-right");
        }

        public void action(ActionEvent e) throws Exception {
            DataModel dataModel = new DataModel(getDocumentContext(e), getEditor().getProperties());

            int pos = getEditorPane(e).getCaretPosition();

            getDefaultCaretRightAction().actionPerformed(e);

            if (getEditor().getMode() == VirtualEditor.MODE_VIRTUAL_PAGE_DATA_EDITOR && dataModel.isInTemplate(pos)) {
                dataCaretAction.adjustDataFocus();
                return;
            }

            getEditor().fireUserCaretEvent();

        }
    }

    public static class VirtualCaretUpAction extends CaretUpAction {

        static TableActions.TableCaretUpAction tableCaretUpAction = null;
        static TableActions.TableCaretEnterAction tableCaretEnterAction = null;
        static DataActions.CommonCaretAction dataCaretAction = null;

        public VirtualCaretUpAction(Editor editor) {
            super(editor);
            tableCaretUpAction = new TableActions.TableCaretUpAction(getDefaultCaretUpAction(), editor);
            tableCaretEnterAction = new TableActions.TableCaretEnterAction(getDefaultCaretUpAction(), editor);
            dataCaretAction = new DataActions.CommonCaretAction((VirtualEditor) editor, "data-caret-up");
        }

        public void action(ActionEvent e) throws Exception {
            TableModel tableModel = new TableModel(getDocumentContext(e));
            DataModel dataModel = new DataModel(getDocumentContext(e), getEditor().getProperties());

            int pos = getEditorPane(e).getCaretPosition();

            if (tableModel.isTable(pos)) {
                int oldPos = -1;
                if (getEditor().getMode() == VirtualEditor.MODE_VIRTUAL_PAGE_DATA_EDITOR && dataModel.isInTemplate(pos)) {
                    oldPos = pos;
                }

                tableCaretUpAction.action(e);

                if (oldPos >= 0) {
                    dataCaretAction.adjustDataFocus(oldPos);
                }
                return;
            }

            getDefaultCaretUpAction().actionPerformed(e);

            if (getEditor().getMode() == VirtualEditor.MODE_VIRTUAL_PAGE_DATA_EDITOR && dataModel.isInTemplate(pos)) {
                dataCaretAction.adjustDataFocus();
                return;
            }

            if (tableModel.isTable(getEditorPane(e).getCaretPosition())) {
                tableCaretEnterAction.action(e);
            }
        
            getEditor().fireUserCaretEvent();
        }
    }

    public static class VirtualCaretDownAction extends CaretDownAction {

        static TableActions.TableCaretDownAction tableCaretDownAction = null;
        static TableActions.TableCaretEnterAction tableCaretEnterAction = null;
        static DataActions.CommonCaretAction dataCaretAction = null;

        public VirtualCaretDownAction(Editor editor) {
            super(editor);
            tableCaretDownAction = new TableActions.TableCaretDownAction(getDefaultCaretDownAction(), editor);
            tableCaretEnterAction = new TableActions.TableCaretEnterAction(getDefaultCaretDownAction(), editor);
            dataCaretAction = new DataActions.CommonCaretAction((VirtualEditor) editor, "data-caret-down");
        }

        public void action(ActionEvent e) throws Exception {
            TableModel tableModel = new TableModel(getDocumentContext(e));
            DataModel dataModel = new DataModel(getDocumentContext(e), getEditor().getProperties());

            int pos = getEditorPane(e).getCaretPosition();

            if (tableModel.isTable(pos)) {
                int oldPos = -1;
                if (getEditor().getMode() == VirtualEditor.MODE_VIRTUAL_PAGE_DATA_EDITOR && dataModel.isInTemplate(pos)) {
                    oldPos = pos;
                }

                tableCaretDownAction.action(e);

                if (oldPos >= 0) {
                    dataCaretAction.adjustDataFocus(oldPos);
                }
                return;
            }

            getDefaultCaretDownAction().actionPerformed(e);

            if (getEditor().getMode() == VirtualEditor.MODE_VIRTUAL_PAGE_DATA_EDITOR && dataModel.isInTemplate(pos)) {
                dataCaretAction.adjustDataFocus();
                return;
            }

            if (tableModel.isTable(getEditorPane(e).getCaretPosition())) {
                tableCaretEnterAction.action(e);
            }
            
            getEditor().fireUserCaretEvent();
        }
    }
    
}
